//
//  ISHomeCollectionViewCell.m
//  iPhoneSafety
//
//  Created by dlon 2018/12/30.
//  Copyright © 2018年 com.li.ding. All rights reserved.
//

#import "ISHomeCollectionViewCell.h"
#import <Masonry/Masonry.h>
#import "NWLazyProperty.h"
#import "UIColor+NWHex.h"
#import "UIView+BlocksKit.h"

@interface ISHomeCollectionViewCell()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIView *bgShadowView;
@property (nonatomic, strong) UIView *switchBgView;
@end

@implementation ISHomeCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
       
        [self.contentView addSubview:self.bgShadowView];
        [self.contentView addSubview:self.bgView];
        
        [self.contentView addSubview:self.imgView];
        [self.contentView addSubview:self.titleLabel];
        
        [self.contentView addSubview:self.desLabel];
        [self.contentView addSubview:self.switchBgView];
        [self.switchBgView addSubview:self.switchView];
        
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(7, 6, 7, 6));
        }];
        
        [self.bgShadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.bgView);
        }];
        
        [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).offset(60.f);
            make.left.right.equalTo(self.bgView);
            make.height.equalTo(self.imgView.mas_width).multipliedBy(90.f / 160.f);
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.bgView).insets(UIEdgeInsetsMake(0, 16.f, 0, 0));
            make.bottom.equalTo(self.imgView.mas_top);
        }];
        
        [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).insets(UIEdgeInsetsMake(0, 15, 0, 15));
            make.top.equalTo(self.imgView.mas_bottom).offset(7.f);
        }];
        
        [self.switchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.equalTo(self.contentView);
            make.height.equalTo(@(36.f + 24.f));
            make.width.equalTo(@(20.f + 20.f));
        }];
        [self.switchView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.switchBgView);
        }];
    }
    return self;
}

#pragma mark - set & get

NWLazyPropertyWithInit(UIView, bgView, {
    _bgView.layer.cornerRadius = 6.f;
    _bgView.clipsToBounds = YES;
    _bgView.backgroundColor = [UIColor whiteColor];
})

NWLazyPropertyWithInit(UIView, bgShadowView, {
    _bgShadowView.backgroundColor = [UIColor whiteColor];
    _bgShadowView.layer.cornerRadius = 6.f;
    _bgShadowView.layer.shadowColor = [UIColor nw_colorWithHex:0x18191A alpha:0.12].CGColor;
    _bgShadowView.layer.shadowOffset = CGSizeMake(0,12);
    _bgShadowView.layer.shadowOpacity = 1.f;
    _bgShadowView.layer.shadowRadius = 12.f;
})

NWLazyPropertyWithInit(UIImageView, imgView, {})

NWLazyPropertyWithInit(UILabel, titleLabel, {
    _titleLabel.font = [UIFont boldSystemFontOfSize:18.f];
    _titleLabel.textColor = [UIColor nw_colorWithHex:0x222222];
    _titleLabel.numberOfLines = 1;
})

NWLazyPropertyWithInit(UILabel, desLabel, {
    _desLabel.font = [UIFont systemFontOfSize:14.f];
    _desLabel.textColor = [UIColor nw_colorWithHex:0x979CA7];
    _desLabel.numberOfLines = 3;
    _desLabel.textAlignment = NSTextAlignmentCenter;
})
NWLazyPropertyWithInit(UIView, switchBgView, {
    @weakify(self)
    [_switchBgView bk_whenTapped:^{
        @strongify(self)
        self.switchView.on = !self.switchView.on;
        !self.actionBlock ?: self.actionBlock(self.switchView.isOn);
    }];
})
NWLazyPropertyWithInit(UISwitch, switchView, {
    _switchView.onTintColor = [UIColor nw_colorWithHex:0x00AEFF];
    _switchView.tintColor = [UIColor nw_colorWithHex:0xDDE9F0];
    _switchView.backgroundColor = [UIColor nw_colorWithHex:0xDDE9F0];
    _switchView.clipsToBounds = YES;

    _switchView.layer.cornerRadius = CGRectGetHeight(_switchView.frame) / 2.f;
    CGAffineTransform scale = CGAffineTransformMakeScale(36.f / CGRectGetWidth(_switchView.frame), 20.f / CGRectGetHeight(_switchView.frame));
    CGAffineTransform rotation = CGAffineTransformMakeRotation(-M_PI / 2.f);
    _switchView.transform = CGAffineTransformConcat(scale, rotation);
    
    _switchView.userInteractionEnabled = NO;
})

@end
